<?php
namespace Admin\Model;
use Think\Model;

class ProjectUserRelModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE,'',1),
        array('UserID','number','请正确选择参与者人员',Model::MUST_VALIDATE,'',1),
        array('JobDuty','require','请填写角色/职责',Model::MUST_VALIDATE,'',1)
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		$where = " 1 ";
		foreach($parmArr AS $key => $value){
			$where .= " AND a.$key = '$value'"; 
		}
		$sql = "SELECT a.IsPM,a.Memo,a.JobDutyDetail,a.ID,a.JobDuty,a.ProjectID,c.UserName,a.UserID FROM project_user_rel AS a LEFT JOIN user AS c ON a.UserID = c.ID  WHERE $where";
		$res = $this->query($sql);
		return $res;
	}
	
	public function MyshowList(){
		$where = " 1 ";
		$keyword = I('keyword');
		if(!empty($keyword)){
			$sql = "SELECT ID FROM project WHERE Name = '{$keyword}'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND ProjectID = ".$res[0]['ID'];
			}else{
				$where .= " AND ProjectID = -1";
			}
		}
		$return = array();
		$sql = " SELECT COUNT(*) count FROM project_user_rel WHERE $where ";
		$count = $this->query($sql);
		$count = $count[0]['count'];
	    $page = $this->Mypage($count, 8);
		$sql = "SELECT  *, (SELECT Name FROM project WHERE ID = a.ProjectID) AS ProjectName, (SELECT UserName FROM user WHERE ID = a.UserID) AS UserName FROM project_user_rel AS a WHERE $where ORDER BY ID DESC LIMIT  $page->firstRow , $page->listRows";
		$res = $this->query($sql);
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
	public function getTableData(){
		$dataArr = array();
		$join1 = "LEFT JOIN user as b ON a.UserID = b.ID";
		$res1 = $this->alias('a')->field("a.*,b.UserName,b.Phone")->join($join1)->where(array("ProjectID"=>$_GET['id']))->order("ID DESC")->select();
		foreach($res1 AS $k => $v){
			$idStr = "";
			$nameStr = "";
			if(!empty($v['UserID'])){
				$res2 = M("user_dep_rel")->alias('a')->field("b.Name,b.ID")->join("LEFT JOIN department AS b ON a.DepID = b.ID")->where(array("UserID"=>$v['UserID']))->select();
				foreach($res2 AS $K => $V){
					if(!empty($V['ID']))$idStr .= $V['ID'].',';
					if(!empty($V['Name']))$nameStr .= $V['Name'].',';
				}
				$res1[$k]['DepName'] = substr($nameStr,0,-1);
				$res1[$k]['DepID'] = substr($idStr,0,-1);
			}
		}
		$dataArr['data'] = $res1;
		$dataArr['projectID'] = $_GET['id'];
		$res2 = M("project")->field("Name")->where(array("ID"=>$_GET['id']))->find();
		$dataArr['projectName'] = $res2['Name'];
		return $dataArr;
	}
	
}

?>				